import { Injectable } from '@angular/core';
import { Observable, of } from 'rxjs';
import { delay,tap } from 'rxjs/operators';
import * as gc from 'src/app/global/const';
import { Router } from '@angular/router';
import { HttpClient } from '@angular/common/http';

/**
 * 2019年12月1日21:00:00
 * 本服务提供认证与授权的功能
 */
@Injectable({
  providedIn: 'root'
})
export class AuthService {
  isLoggedIn = true;
  redirectUrl: string;

  urlPre = "/login";
  url = {
    'login':gc.BACKEND_SERVER + this.urlPre + '/in',
    'logout':gc.BACKEND_SERVER + this.urlPre + '/out'
  }

  constructor(
    private router:Router,
    private http:HttpClient
  ) { }


  /**
   * 登录成功的话返回对象是：{code: 1, status: "ok", msg: "登录成功", data: null}
   * @param userid 工号，在SpringSecurity5中对应为 username
   * @param password 密码
   */
  login(userid:string,password:string):Observable<any>{
    let user:SysUser = new SysUser();
    user.userid = userid;
    user.password = password;
    user.username = userid;
    
    console.log(user);
    console.log(this.url.login);

    // let headers = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8' });
    // return this.http.post(this.url.login,{'username':userid,'password':password},{'headers':headers});
    // 下面的两个 key 名称不可修改，对应后端接口的属性名称
    return this.http.post(this.url.login,{'username':userid,'password':password});
  }


  // 跳转到首页
  gotoIndex(){
    this.router.navigate(['/layout']);
  }

  // 退出
  logout(): void {
    this.isLoggedIn = false;
  }
}


// Author：chanchaw
// Generator Version：1.0 - 2019年11月16日 16点46分
// This Model Created：Sat Nov 30 2019 08:29:19 GMT+0800 (中国标准时间)
export class SysUser{
  create_time:Date;
  iid:number;
  last_time:Date;
  non_expired:boolean;
  non_locked:boolean;
  non_password_expired:boolean;
  password:string;
  remark:string;
  status:number;
  userid:string;
  username:string;
}



  // 登录
  // login(): Observable<boolean> {
  //   return of(true).pipe(
  //     delay(1000),
  //     tap(val => this.isLoggedIn = true)
  //   );
  // }

